<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

    <!--引入自定义文件   注意：要将他引入到jquery的下方  否则不生效-->
    <script src="../js/pages/public.js"></script>
</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->

<div class="box-header with-border">
    <h3 class="box-title">商品审核</h3>
</div>

<div class="box-body">

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" onclick="deleteGoods()" title="删除"><i
                            class="fa fa-trash-o"></i> 删除
                    </button>
                    <button type="button" class="btn btn-default" onclick="UpdateStatus()" title="审核通过"><i
                            class="fa fa-check"></i> 审核通过
                    </button>
                    <button type="button" class="btn btn-default" title="驳回"><i class="fa fa-ban"></i> 驳回</button>
                    <button type="button" class="btn btn-default" onclick="location.reload()" title="刷新"><i
                            class="fa fa-refresh"></i> 刷新
                    </button>
                </div>
            </div>
        </div>
        <div class="box-tools pull-right">
            <div class="has-feedback">

                <form id="formId">
                    商品名称：<input name="goodsName" class="form-control">
                    <button class="btn btn-default" onclick="FindGoodsName()">查询</button>
                </form>


            </div>
        </div>
        <!--工具栏/-->

        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">商品ID</th>
                <th class="sorting">商品名称</th>
                <th class="sorting">商品价格</th>
                <th class="sorting">一级分类</th>
                <th class="sorting">二级分类</th>
                <th class="sorting">三级分类</th>
                <th class="sorting">状态</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="GoodsIds">
            </tbody>
        </table>
        <!--数据列表/-->


    </div>
    <!-- 数据表格 /-->
    <form id="indexForm">
        <a href="javascript:toPage(3)">首页</a>
        <a href="javascript:toPage(1)">上一页</a>
        <a href="javascript:toPage(2)">下一页</a>
        <a href="javascript:toPage(4)">尾页</a>
        第<input type="text" name="pageNum" id="pageNum" size="1px" readonly="readonly">页
        共<input type="text" name="pages" id="pages" size="1px" disabled="disabled">页
        共<input type="text" name="total" id="total" size="1px" disabled="disabled">条
    </form>

</div>
<!-- /.box-body -->

</body>
<script th:inline="javasrcipt">//th:inline=”javascript” 在 js 代码中获取后台的动态数据。
$(function () {
    getGoodsList()
})

function getGoodsList() {
    $.ajax({
        url: goods + "/goods/getGoodsList",
        headers: {"Autherizetion": window.localStorage.getItem("token")},//获取浏览器中请求头的token  获取到token之后可以直接通过网关过滤器的请求
        // contentType: "application/json;charset=utf-8",//往后端发送的数据的格式
        dataType: "json",//参数类型
        type: "post",//请求类型
        data: $("#indexForm").serialize(),//说明往后端发送的数据确确实实就是一个json数据  JSON.stringify({})
        success: function (result) {
            if (result.code == 11) {
                // console.log(result)
                //拼接
                var data = result.data.rows;
                var html = '';
                $(data).each(function (i, e) {
                    html += '<tr>\n' +
                        '                <td><input name="check" value="' + e.id + '" type="checkbox"></td>\n' +
                        '                <td>' + e.id + '</td>\n' +
                        '                <td>' + e.goodsName + '</td>\n' +
                        '                <td>' + e.price + '</td>\n';
                    html += '<td>' + e.category1Id + '</td>'
                    html += '<td>' + e.category2Id + '</td>'
                    html += '<td>' + e.category3Id + '</td>'
                    if (e.auditStatus == 0) {
                        html += '<td>待审核</td>'
                    } else if (e.auditStatus == 1) {
                        html += '<td>审核通过</td>'
                    } else if (e.auditStatus == 2) {
                        html += '<td>审核不通过</td>'
                    }
                    html += '                <td class="text-center">\n' +
                        '                    <button type="button" class="btn bg-olive btn-xs">修改</button>\n' +
                        '                </td>\n' +
                        '            </tr>';
                })
                $("#GoodsIds").html(html)
                $("#pageNum").val(result.data.pageNum)
                $("#pages").val(result.data.pages)
                $("#total").val(result.data.total)

            } else if (result.code == 12) {
                top.window.location.href = "../login.html";
            }
        }, error: function () {
            alert("查询商品管理异常")
        }
    })
}

/*分页*/
function toPage(id) {
    var pageNum = parseInt($("#pageNum").val());
    var pages = parseInt($("#pages").val());
    if (id == 1) {
        if (pageNum <= 1) {
            alert("已是首页");
            return;
        }
        $("#pageNum").val(pageNum - 1);
    } else if (id == 2) {
        if (pageNum >= pages) {
            alert("已是尾页");
            return;
        }
        $("#pageNum").val(pageNum + 1);
    } else if (id == 3) {
        $("#pageNum").val(1);
    } else if (id == 4) {
        $("#pageNum").val(pages);
    }
    getGoodsList();
}

//删除
function deleteGoods() {
    var arr = [];
    var check = document.getElementsByName("check");
    for (let i = 0; i < check.length; i++) {
        if (check[i].checked) {
            arr.push(check[i].value)
        }
    }
    if (arr <= 0) {
        alert("请选择要删除的数据")
        return;
    }
    if (confirm("确认要删除吗？")) {
        console.log(arr)
        $.ajax({
            url: goods + "/goods/delteGoods/" + arr,
            headers: {'Autherizetion': window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "delete",//必须指定请求类型
            // data:{arr:arr},//往后台发送的确确实实是一个json数据
            success: function (result) {
                if (result.code == 11) {
                    alert("删除成功")
                    location.reload();//刷新
                } else if (result.code == 12) {
                    top.window.location.href = "../shoplogin.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }
        })
    }
}

function UpdateStatus() {
    var arr = [];
    var check = document.getElementsByName("check");
    for (let i = 0; i < check.length; i++) {
        if (check[i].checked) {
            arr.push(check[i].value)
        }
    }
    if (arr <= 0) {
        alert("请选择要审核的数据")
        return;
    }
    console.log(arr)
    $.ajax({
        url: goods + "/goods/updateGoods/" + arr,
        headers: {'Autherizetion': window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
        contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
        dataType: "json",
        type: "put",//必须指定请求类型
        // data:{arr:arr},//往后台发送的确确实实是一个json数据
        success: function (result) {
            if (result.code == 11) {
                alert("审核成功")
                location.reload();//刷新
            } else if (result.code == 12) {
                top.window.location.href = "../shoplogin.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
            }
        }
    })
}

// function FindGoodsName() {
//     var goodsname = $("#formId").serializeObject();
//     console.log(goodsname)
//     $.ajax({
//         url: domain + "/goods/FindGoodsName",
//         headers: {'Autherizetion': window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
//         contentType: "application/json;charset=utf-8",
//         dataType: "json",
//         type: "post",
//         data: JSON.stringify(goodsname),
//         success: function (result) {
//             alert("11111111")
//                 console.log(result)
//             if (result.code == 11) {
//
//             } else if (result.code == 12) {
//                 top.window.location.href = "../shoplogin.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
//             }
//         }
//     })
// }
</script>
</html>